const fs = require('fs');
const path = require('path');
const { compatibilityTree } = require('../src/compatibility-tree');

// 确保兼容层目录存在
if (!fs.existsSync('compatibility-layer')) {
  fs.mkdirSync('compatibility-layer');
}

// 初始化环境
async function initializeEnvironment() {
  try {
    console.log('Initializing compatibility tree environment...');
    
    // 创建版本化目录结构
    console.log('Creating versioned directories...');
    compatibilityTree.buildCompatibleEnvironment();
    
    // 复制配置文件到兼容层
    console.log('Setting up configuration files...');
    copyConfigFiles();
    
    // 创建环境标记
    fs.writeFileSync(
      path.join('compatibility-layer', '.initialized'),
      `Compatibility Tree initialized at ${new Date().toISOString()}`
    );
    
    console.log('Compatibility tree environment initialized successfully!');
  } catch (error) {
    console.error('Failed to initialize environment:', error);
    process.exit(1);
  }
}

// 复制配置文件到兼容层
function copyConfigFiles() {
  const configFiles = [
    '.env.example',
    '.eslintrc.json',
    '.prettierrc.json',
    'jest.config.ts',
    'tsconfig.json'
  ];

  configFiles.forEach(file => {
    if (fs.existsSync(file)) {
      fs.copyFileSync(
        file,
        path.join('compatibility-layer', file)
      );
    }
  });
}

// 执行初始化
initializeEnvironment();